﻿#include "pch.h"
#if _QG_ENABLE_WIDGET
#include "qgp/qgach.h"
#include "qgp/qgaud.h"
#include "qgp/qguwgs.h"

//////////////////////////////////////////////////////////////////////////
// 버튼
KIMPL_SUBCLASS(qwRibbonCombo, "WidgetRibbonCombo");

qwRibbonCombo::qwRibbonCombo(qgWidgetType type)
	: super_type(type)
	, _dir(QGWDIR_HORIZ)
	, _icon_coord(tpl::Quad::FillCoord())
{
	_insts.fill = true;
	_insts.input = true;
	_insts.ribbon = true;

	//SetColor(QSWGCD_SELECT, SbColor(0.35f, 0.35f, 0.35f, 0.5f));
	//SetColor(QSWGCD_OVER, SbColor(0.431f, 0.592f, 0.482f, 0.8f));
	//SetColor(QSWGCD_DISABLE, SbColor(0.0f, 0.0f, 0.0f, 1.0f));

	SetTextSize(12);
}

qwRibbonCombo::~qwRibbonCombo()
{
	_cbo->Unload();
}

bool qwRibbonCombo::OnUpdate(float adv)
{
	BuildBound();

	if (NeedRefresh())
	{
		_cbo->SetRefresh(true);

		if (_dir == QGWDIR_HORIZ)
		{
			// 글자 크기 대 아이콘 크기의 대결
			kint m = K_MAX(_text_info.size, 16);

			_bound.bottom = _bound.top + m + 6;
			BuildBound();

			kint w = _bound.Width();
			//kint h = _bound.Height();

			_cbo->SetBound(16 + 3, _bound.top, w - (16 + 3 + 3), m);
			_cbo->SetScrollSize(_text_info.size);

			_icon_bound.Set(_client.left + 1, _client.top + 3,
				_client.left + 16 + 1, _client.top + 16 + 3);
		}
		else if (_dir == QGWDIR_VERT)
		{
			_bound.bottom = _bound.top + 66;
			BuildBound();

			kint w = _client.Width();

			_cbo->SetBound(3, 32 + 4 + 3, w - 6, _text_info.size);
			_cbo->SetScrollSize(_text_info.size);

			kint m = (w - 32) / 2;
			_icon_bound.Set(_client.left + m, _client.top + 3,
				_client.left + m + 32, _client.top + 32 + 3);
		}

		SetRefresh(false);
	}

	_cbo->OnUpdate(adv);

	return true;
}

void qwRibbonCombo::OnDraw()
{
	if (CanFill())
	{
		// 배경
		if (GetStatus() == QGWS_NORMAL)
			_shed->DrawFillRect(_client, GetResource());

		// 아이콘
		if (_tex)
			_shed->DrawFillRect(_icon_bound, tpl::ColorValues::White, _tex);
	}

	if (HasBorder())
		_shed->DrawEmptyRect(_client, _color_border);

	_cbo->OnDraw();

	qwCtrl::OnDraw();
}

//
namespace _qgf
{
	class RibbonComboEvent : public qwRibbonCombo
	{
	public:
		void OnEventOpenDrop(qwCtrl* wg, bool value)
		{
			if (_ecb_context_open)
				_ecb_context_open(this, value);
		}

		void OnEventCloseDrop(qwCtrl* wg, bool value)
		{
			if (_ecb_context_close)
				_ecb_context_close(this, value);
		}

		void OnEventSelectIndexChanged(qwCtrl* wg, kint index)
		{
			if (_ecb_select_changed)
				_ecb_select_changed(this, index);
		}

		void DoSetEvents()
		{
			_cbo->SetEventContextOpen(qwEventBoolFunc(this, &RibbonComboEvent::OnEventOpenDrop));
			_cbo->SetEventContextClose(qwEventBoolFunc(this, &RibbonComboEvent::OnEventCloseDrop));
			_cbo->SetEventSelectChanged(qwEventIntFunc(this, &RibbonComboEvent::OnEventSelectIndexChanged));
		}
	};
}

//
void qwRibbonCombo::OnInitialize()
{
	_cbo = (qwCombo*)_shed->CreateControl(QGW_COMBO, 0, tpl::Rect(0, 0, 0, 0), NULL);
	_cbo->SetParent(this);
	_cbo->SetTextSize(_text_info.size);
	_cbo->SetItemHeight(12);
	_cbo->SetFillMode(false);

	static_cast<_qgf::RibbonComboEvent*>(this)->DoSetEvents();
}

//
void qwRibbonCombo::SetDirection(qgWidgetDirection dir)
{
	if (dir == QGWDIR_HORIZ || dir == QGWDIR_VERT)
		_dir = dir;
}

//
void qwRibbonCombo::ClearItems()
{
	_cbo->ClearItems();
}

//
bool qwRibbonCombo::AddItem(const char* str, kpointer tag)
{
	return _cbo->AddItem(str, tag);
}

//
bool qwRibbonCombo::AddItem(const char* str, const tpl::ColorValue& color, kpointer tag)
{
	return _cbo->AddItem(str, color, tag);
}

//
bool qwRibbonCombo::InsertItem(kint nth, const char* str, kpointer tag)
{
	return _cbo->InsertItem(nth, str, tag);
}

//
bool qwRibbonCombo::InsertItem(kint nth, const char* str, const tpl::ColorValue& color, kpointer tag)
{
	return _cbo->InsertItem(nth, str, color, tag);
}

//
bool qwRibbonCombo::RemoveItem(kint nth)
{
	return _cbo->RemoveItem(nth);
}

//
qwCombo::Item* qwRibbonCombo::GetItem(kint nth)
{
	return _cbo->GetItem(nth);
}

//
void qwRibbonCombo::OnChangeFocus(bool isfocus)
{
	if (_cbo)
		_cbo->OnChangeFocus(isfocus);
}

//
bool qwRibbonCombo::OnMouseMove(kint x, kint y, const tpl::Point& relative, kushort state)
{
	if (_cbo && !_cbo->OnMouseMove(x, y, relative, state))
		return false;

	return true;
}

//
bool qwRibbonCombo::OnMouseDown(kint x, kint y, kushort button)
{
	if (_cbo)
	{
		if (_cbo->IsOpen())
		{
			if (!_cbo->OnMouseDown(x, y, button))
				return false;

			// 닫자
			_shed->SetFocus(nullptr);
		}
		else
		{
			kint dx = x - _client.left;
			kint dy = y - _client.top;
			if (_cbo->TestPick(dx, dy))
			{
				if (_cbo->OnMouseDown(x, y, button))
					return false;
			}
			else
			{
				// 열자
				_shed->SetFocus(_cbo);
			}
		}
	}

	return true;
}

//
bool qwRibbonCombo::OnMouseUp(kint x, kint y, kushort button)
{
	if (_cbo && !_cbo->OnMouseUp(x, y, button))
		return false;

	return true;
}

//
bool qwRibbonCombo::OnMouseWheel(kshort delta)
{
	if (_cbo && !_cbo->OnMouseWheel(delta))
		return false;

	return true;
}

#endif	// _QG_ENABLE_WIDGET
